const Koa = require('koa')
const app = new Koa()
const cors = require('koa2-cors')
const Router = require('koa-router')
const path = require('path')
const koaBody = require('koa-body');
const koaStatic = require('koa-static')
const getUploadFileExt = require('./uilt/getUploadFileExt');
const checkDirExist = require('./uilt/checkDirExist');
const getUploadFileName = require('./uilt/getUploadFileName');
const server = require('http').Server(app.callback());

app.use(koaBody({
    multipart: true, // 支持文件上传
    formidable:{
        uploadDir: './uploadFile', // 设置文件上传目录
        keepExtensions: true,    // 保持文件的后缀
        maxFieldsSize: 20 * 1024 * 1024, // 文件上传大小
        hash: false, // 计算文件的 hash，则可以选择 md5/sha1
        onFileBegin: (name, file) => {
            // 获取文件后缀
            const ext = getUploadFileExt(file.name);
            // 获取文件名称
            // const fileName = getUploadFileName(ext.type);
            const fileName = ext.name + '.' + ext.type
            // 检查文件夹是否存在如果不存在则新建文件夹
            checkDirExist('./uploadFile');
            // 重新覆盖 file.path 属性
            file.path = `./uploadFile/${fileName}`;
            app.context.uploadpath = app.context.uploadpath ? app.context.uploadpath : {};
            app.context.uploadpath[name] = `/uploadFile/${fileName}`;
            app.context.uploadpath['fileName'] = ext.name;
        },
    }
}));

require('./database/models/organVirusData')
require('./database/models/new')
require('./database/models/user')
require('./database/models/home')
require('./database/models/metadata')
require('./database/models/metamapExpression')
require('./database/models/link')
require('./database/models/tutorial')
require('./database/models/basedata')
require('./database/models/statistics')

const user = require('./appApi/user')
const home = require('./appApi/home')
const news = require('./appApi/new')
const organVirusData = require('./appApi/organVirusData')
const metadata = require('./appApi/metadata')
const link = require('./appApi/link')
const tutorial = require('./appApi/tutorial')
const imageUpload = require('./appApi/imageUpload')
const basedata = require('./appApi/basedata')
// const statistics = require('./appApi/statistics')

let router = new Router()

router.use('/user', user.routes())
router.use('/home', home.routes())
router.use('/new', news.routes())
router.use('/organVirusData', organVirusData.routes())
router.use('/metadata', metadata.routes())
router.use('/link', link.routes())
router.use('/tutorial', tutorial.routes())
router.use('/imageUpload', imageUpload.routes())
router.use('/basedata', basedata.routes())
// router.use('/statistics', statistics.routes())

server.listen(3011, () => {
    console.log(`app run at: http://127.0.0.1:3011`);
})

// 允许跨域
app.use(cors({
    origin: () => '*',
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS', 'PUT'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))

app.use(router.routes())
app.use(router.allowedMethods())
app.use(koaStatic(path.join(__dirname, './public/')))

;(async () =>{

})()
